package com.mzy.javaapi.IO.JSONIO;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.testng.annotations.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/6/20 13:22
 * @Version 1.0
 */
public class Test02 {
    public static void main(String[] args) throws IOException {
        String finalPath = "F:\\每日脚本暂存\\2022.06月\\2022.06.20\\test2.json";
        // 读对应的json
        String string = JsonUtils.readStringFromFile(finalPath, "utf-8");
        // json字符串 转对象数组
        List<Person> personList = JSONObject.parseArray(string, Person.class);

        personList.forEach(e-> System.out.println(e));
//        System.out.println(personList.get(0).getName());

        // 对象转Json
        Person personUpdate = new Person(223993, "王多鱼s3s", 0,null);
        ArrayList<Person> objects = new ArrayList<>();
        objects.add(personUpdate);
        writeModel(finalPath,objects);
    }

    // 将对象list写入json
    public static void writeModel(String finalPath,List<Person> jsonObject){
        try {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(finalPath), StandardCharsets.UTF_8);
            String json = JSONObject.toJSONString(new ArrayList<Person>());


            osw.write(json);
            osw.flush();
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void demo01() throws IOException {
        String finalPath = "F:\\每日脚本暂存\\2022.06月\\2022.06.20\\ATTRIBUTE_SYSTEM.json21";
        // 读对应的json
        try {
            String string = JsonUtils.readStringFromFile(finalPath, "utf-8");
            // 方式1: 将json字符串转为Java对象
            SystemAttribute systemAttribute = JSON.parseObject(string, SystemAttribute.class);
            System.out.println(systemAttribute.getConfigEdition());
        } catch (IOException e) {
            System.out.println("oo"); // 不处理
        }

        System.out.println("?????????");

//        SystemAttribute systemAttribute1 = new SystemAttribute();
//        systemAttribute1.setNodeId("aaa");
//
//        try {
//            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(finalPath), StandardCharsets.UTF_8);
//            osw.write(JSON.toJSONString(systemAttribute1));
//            osw.flush();
//            osw.close();
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }


    }
}
